﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using SharePoint.Contrib.Extensions;
using System.Xml.Linq;
using System.Xml;

namespace NUnit.SharePointContrib
{
    [TestFixture]
    public class XElementExtensionsTest
    {
        XElement xml;
        XDocument doc;

        [SetUp]
        public void Init()
        {
            xml = new XElement("elements",
                    new XElement("element", "First"),
                    new XElement("element", "10"),
                    new XElement("element", "2010-02-02"));

            doc = XDocument.Parse(@"
<runtime>
    <assemblyBinding xmlns=""urn:schemas-microsoft-com:asm.v1"">
      <dependentAssembly>
        <assemblyIdentity name=""System.Web.Extensions"" publicKeyToken=""31bf3856ad364e35""/>
        <bindingRedirect oldVersion=""1.0.0.0-1.1.0.0"" newVersion=""3.5.0.0""/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name=""System.Web.Extensions.Design"" publicKeyToken=""31bf3856ad364e35""/>
        <bindingRedirect oldVersion=""1.0.0.0-1.1.0.0"" newVersion=""3.5.0.0""/>
      </dependentAssembly>
    </assemblyBinding>
  </runtime>");

        }

        [TearDown]
        public void Dispose()
        {
        }

        [Test]
        public void CanConvertElementStringValue()
        {
            var element = (from x in xml.Descendants("element")
                           where x.Value == "First"
                           select x).FirstOrDefault();

            Assert.That(element.OfType<string>(), Is.EqualTo("First"));
        }

        [Test]
        public void CanConvertElementIntegerValue()
        {
            var element = (from x in xml.Descendants("element")
                           where x.Value == "10"
                           select x).FirstOrDefault();

            Assert.That(element.OfType<int>(), Is.EqualTo(10));
        }

        [Test]
        public void CanConvertElementDateTimeValue()
        {
            var element = (from x in xml.Descendants("element")
                           where x.Value == "2010-02-02"
                           select x).FirstOrDefault();

            Assert.That(element.OfType<DateTime>(), Is.EqualTo(new DateTime(2010, 2, 2)));
        }


        [Test]
        public void CanRemoveNameSpace()
        {
            var query = from attr in doc.Root.Descendants().Attributes()
                        where attr.IsNamespaceDeclaration
                        select attr;

            Assert.That(query.FirstOrDefault(), Is.Not.Null);

            var without = from attr in doc.Root.Descendants().Attributes()
                          where attr.IsNamespaceDeclaration
                          select attr.Parent.RemoveNamespace(attr.Name.LocalName);

            var query2 = from attr in without.Attributes()
                        where attr.IsNamespaceDeclaration
                        select attr;

            Assert.That(query2.FirstOrDefault(), Is.Null);

    
        }



    }
}
